/*
 * @Author: 李石
 * @Date: 2024-04-12 17:08:40
 * @LastEditors: lishi
 * @LastEditTime: 2024-04-17 11:44:26
 * @Description: 
 * Copyright (c) 2024 by ${lishi0105@163.com}, All Rights Reserved. 
 */
#ifndef _FILE_PARSE_HPP_
#define _FILE_PARSE_HPP_

#include <vector>
#include <memory>
#include <iostream>
#include "NaluParse.h"
class FileParse
{
public:
    enum FileType
    {
        FILE_H264 = 0,
        FILE_H265 = 1,
        FILE_UNK  = 2,
    };
public:
    using Ptr = std::shared_ptr<FileParse>;
    static Ptr CreateShared(const char* filepath);
    ~FileParse();
    void getVideoInfo(videoinfo_t* videoinfo);
    size_t fileSize() const;
    size_t naluCount() const;
    size_t maxNaluSize() const;
    int32_t width() const;
    int32_t height() const;
    float   fps() const;
    int32_t fileCodecId() const;
    std::string fileCodecName() const;
    void DebugSPS() const;
    void DebugPPS() const;
    void DebugVPS() const;
    size_t getNaluData(size_t index, uint8_t *data, size_t srcSize);
    bool getNaluInfo(size_t index, NALU_t *nalu);
    static enum FileType GetFileType(const char* filepath);
protected:
    FileParse(const char* filepath);
private:
    int findFirstNALU(FILE* fp, int* startcodeLenght);

    FileType judeVideoFile(const char* filename);

    void mergeNalu();

    int parseFile(FILE* fp, std::vector<NALU_t> &);

    int parseNALU(FILE* fp, NALU_t* nalu);

private:
    h264_stream_t* m_hH264;
    h265_stream_t* m_hH265;
    FileType m_nType; // 0:264 1:265
    std::string m_filename;
    size_t max_nalu_size;
    std::vector<NALU_t> m_vNalTypeVector;
};

#endif // _FILE_PARSE_HPP_